<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  <style>
    li {
      list-style: none;
    }

    .center {
      margin: 0 auto;
      width: 400px;
      text-align: center;
    }

    .red {
      color: red;
    }

    .green {
      color: green;
    }
  </style>
</head>

<body>
  <div id="app"></div>
  <template id="root">
    <div class="center">
      <h3>用户注册</h3>
      <ul>
        <li>
          <label>用户名:</label><input type="text" @blur="checkUserName" v-model="username" placeholder="请输入用户名">
          <span :class="[this.errors.username=='校验通过'?'green':'red']">{{errors.username}}</span>
        </li>
        <li>
          <label>密码:</label><input type="password" @blur="checkPass" v-model=" password" placeholder="请输入密码">
          <span>{{errors.password}}</span>
        </li>
        <li>
          <label>性别:</label>
          <input type="radio" name="gender" value="male" v-model="gender" />男
          <input type="radio" name="gender" value="female" v-model="gender" />女
        </li>
        <li>
          <label>地址:</label>
          <select v-model="selectedCity">
            <option :value="c.name" v-for="c in cities">{{c.text}}</option>
          </select>
        </li>
      </ul>
      <button @click="handleRegister">注册</button>
      <div style="display: flex;flex-direction: row;justify-content: center;">
        <table border="1">
          <tr>
            <td>序号</td>
            <td>用户名</td>
            <td>性别</td>
            <td>地址</td>
          </tr>
          <tr v-for="(u,index) in users">
            <td>{{index+1}}</td>
            <td>{{u.username}}</td>
            <td>{{u.gender=='male'?'男':'女'}}</td>
            <td>{{handleCity(u.city)}}</td>
          </tr>
        </table>
      </div>

    </div>
  </template>
</body>
<script>
  const app = Vue.createApp({
    template: "#root",
    data() {
      return {
        username: '',
        password: '',
        gender: 'male',
        cities: [
          { name: 'cq', text: '重庆' },
          { name: 'bj', text: '北京' },
        ],
        selectedCity: 'cq',
        errors: {},
        users: [],
        id: ''
      }
    },
    methods: {
      handleModify() {
        const user = this.users.find((u) => { return u.id == this.id })
        user.username = this.username
        user.password = this.password
        user.gender = this.gender
        user.city = this.selectedCity
      },
      handleRegister() {
        if (this.errors.username == '校验通过' && this.errors.password == '校验通过') {
          console.log({
            username: this.username,
            password: this.password,
            gender: this.gender,
            city: this.selectedCity
          })
          this.users.push({
            username: this.username,
            password: this.password,
            gender: this.gender,
            city: this.selectedCity
          })
        }
      },
      checkUserName() {
        if (this.username == "" || this.username == null) {
          this.errors.username = "用户名不能为空"
        } else {
          this.errors.username = "校验通过"
        }
      },
      checkPass() {
        if (this.password == "" || this.password == null) {
          this.errors.password = "密码不能为空"
        } else {
          if (this.password.length < 6) {
            this.errors.password = "密码长度不正确"

          } else {
            this.errors.password = "校验通过"

          }
        }
      },
      handleCity(name) {
        let text = ''
        this.cities.forEach(c => {
          if (c.name == name) {
            text = c.text
          }
        });
        return text
      }
    }
  })
  app.mount("#app")
</script>

</html>